var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/table/util.ts
var util_exports = {};
__export(util_exports, {
  IUPComponent: () => import_util2.IUPComponent,
  Layout: () => import_util2.Layout,
  Observer: () => import_util.Observer,
  ValueTypeEnum: () => import_util.ValueTypeEnum,
  clientColumnFilter: () => clientColumnFilter,
  compHoc: () => import_util2.compHoc,
  convertStyle: () => convertStyle,
  cssVar: () => import_util2.cssVar,
  dataSortOrder: () => dataSortOrder,
  displayText: () => displayText,
  domContains: () => domContains,
  emptyFn: () => import_util.emptyFn,
  filterSymbol: () => filterSymbol,
  getCellInfoByEvent: () => getCellInfoByEvent,
  getColumnHeader: () => getColumnHeader,
  getDisplayField: () => getDisplayField,
  getGlobalConfig: () => import_util.getGlobalConfig,
  getRegisterComponentWithProps: () => import_util2.getRegisterComponentWithProps,
  getRender: () => getRender,
  getScrollBarInfo: () => getScrollBarInfo,
  stopPropagation: () => stopPropagation,
  syncDefaultProps: () => syncDefaultProps,
  useApi: () => import_util2.useApi,
  useAsyncEffect: () => import_util2.useAsyncEffect,
  useDebounce: () => import_util2.useDebounce,
  useDevState: () => import_util2.useDevState,
  useDraggableSort: () => import_util2.useDraggableSort,
  useIUPEffect: () => import_util2.useIUPEffect,
  useRefCallback: () => import_util2.useRefCallback,
  useRefState: () => import_util2.useRefState,
  useResize: () => import_util2.useResize,
  useReturnCallback: () => import_util2.useReturnCallback,
  useThrottle: () => import_util2.useThrottle,
  useUpdateEffect: () => import_util2.useUpdateEffect,
  util: () => import_util.iup
});
module.exports = __toCommonJS(util_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_icon = require("../../icon");
var import_util = require("../../util");
var import_common = require("./base/common");
var import_util2 = require("../../util");
function getScrollBarInfo() {
  return { width: 8, height: 8 };
}
function getColumnHeader(column) {
  var _a, _b;
  const pageLang = (_b = (_a = import_util.iup).getPageLang) == null ? void 0 : _b.call(_a);
  const header = column.header || column.title;
  if (pageLang && column.langKey) {
    return pageLang[column.langKey] || header;
  }
  return header;
}
function convertStyle(style = {}) {
  const flexStyle = { left: "flex-start", right: "flex-end", center: "center" };
  if (style && style.textAlign) {
    style.justifyContent = flexStyle[style.textAlign];
  }
  return style;
}
function displayText(text) {
  return import_util.iup.isArray(text) ? text.join(",") : (text == null ? void 0 : text.label) ?? import_util.iup.jsonString(text) ?? "";
}
function domContains(parentClass, target) {
  while (target) {
    if (target.classList.contains(parentClass)) {
      return true;
    }
    target = target.parentElement;
  }
  return false;
}
function compareFn(a, b) {
  return a === b ? 0 : a > b ? 1 : -1;
}
function getObjectKey(obj) {
  return Object.keys(obj)[0];
}
function dataSortOrder(data, orders, dataIndexMap) {
  const orderBy = orders.filter((o) => o.local);
  const len = orderBy.length;
  if (len === 0)
    return data;
  return data.slice().sort((a, b) => {
    for (let i = 0; i < len; i++) {
      const current = orderBy[i];
      const dataIndex = getObjectKey(current);
      const di = dataIndexMap[dataIndex].dataIndexField || dataIndex;
      const [valueA, valueB] = [import_util.iup.getObjValue(a, di), import_util.iup.getObjValue(b, di)];
      const compare = current[dataIndex] ? current[dataIndex] === "asc" ? compareFn(valueA, valueB) : compareFn(valueB, valueA) : 0;
      if (compare !== 0) {
        return compare;
      }
    }
    return 0;
  });
}
function filterSymbol(obj) {
  const newObj = {};
  Object.keys(obj).forEach((p) => {
    newObj[p] = obj[p];
  });
  return newObj;
}
function stopPropagation(e) {
  var _a, _b, _c;
  (_a = e == null ? void 0 : e.stopPropagation) == null ? void 0 : _a.call(e);
  (_c = (_b = e == null ? void 0 : e.nativeEvent) == null ? void 0 : _b.stopImmediatePropagation) == null ? void 0 : _c.call(_b);
}
function wrapHtml(children, { tag = "div", className = "nowrap" } = {}) {
  return children ? import_react.default.createElement(tag, { className, children }) : children;
}
function getRender({
  format,
  editor,
  render,
  encrypted
}) {
  if (encrypted) {
    return ({ value }) => "*****";
  } else if (format) {
    const { type, formatter, fileTypeCode, strictly = false, precision, prefix, suffix, nullValue = "" } = format;
    switch (type) {
      case "date":
        return ({ value }) => wrapHtml(import_util.iup.formatDate(value, formatter) || nullValue);
      case "option": {
        let options = formatter;
        let [valueField, labelField] = ["value", "label"];
        if (!options && ((editor == null ? void 0 : editor.data) || (editor == null ? void 0 : editor.options)) && ["select", "Select"].includes(editor.xtype)) {
          options = editor.data || editor.options;
          if (editor.valueField) {
            valueField = editor.valueField;
          }
          if (editor.labelField) {
            labelField = editor.labelField;
          }
        }
        return ({ value }) => {
          const arr = import_util.iup.isString(value) ? value.split(",") : import_util.iup.isArray(value) ? value : [value];
          const labels = [];
          (options || []).forEach((d) => {
            var _a, _b, _c, _d;
            if (arr.includes(d[valueField]) || strictly === false && (arr.includes(+d[valueField]) || arr.includes(d[valueField] + ""))) {
              if (["success", "processing", "error", "default", "warning"].includes(d.status)) {
                if (((_b = (_a = d.type) == null ? void 0 : _a.toLowerCase) == null ? void 0 : _b.call(_a)) === "tag") {
                  return labels.push(
                    import_react.default.createElement(import_antd.Tag, { key: d[valueField], color: d.status, children: d[labelField] })
                  );
                }
                return labels.push(
                  import_react.default.createElement(import_antd.Badge, { key: d[valueField], status: d.status, text: d[labelField] })
                );
              } else if (d.color) {
                if (((_d = (_c = d.type) == null ? void 0 : _c.toLowerCase) == null ? void 0 : _d.call(_c)) === "tag") {
                  return labels.push(
                    import_react.default.createElement(import_antd.Tag, { key: d[valueField], color: d.color, children: d[labelField] })
                  );
                }
                return labels.push(
                  import_react.default.createElement(import_antd.Badge, { key: d[valueField], color: d.color, text: d[labelField] })
                );
              } else {
                labels.push(d[labelField]);
              }
            }
          });
          if (labels.length > 0 && labels.every((v) => !import_util.iup.isObject(v))) {
            return wrapHtml(labels.join(","));
          }
          return wrapHtml(labels.length > 0 ? labels : nullValue || value, {
            className: `${labels.length > 0 ? "" : "nowrap"}`
          });
        };
      }
      case "icon": {
        return ({ value }) => {
          const options = formatter || [];
          const { label, icon, style } = options.find((o) => o.value == value) || {};
          if (label || icon) {
            const tmp = [];
            if (import_react.default.isValidElement(icon)) {
              tmp.push(icon);
            } else if (import_icon.IUPIcon[icon]) {
              tmp.push(
                import_react.default.createElement(import_icon.IUPIcon[icon], {
                  key: tmp.length + 1,
                  style: {
                    color: "var(--primary-color)",
                    marginRight: `${label ? 4 : 0}px`,
                    ...style
                  }
                })
              );
            }
            label && tmp.push(label);
            return tmp;
          }
          return nullValue;
        };
      }
      case "attachment":
        return (params) => {
          var _a;
          const { value: v, dataIndex, row, rowIndex } = params;
          const { icon = "PaperClipOutlined", showTip = "附件", style } = formatter || {};
          const displayField = (editor == null ? void 0 : editor.nameField) || `${dataIndex}FileCount`;
          const value = row[displayField] ?? 0;
          if (value || ((_a = editor == null ? void 0 : editor.listeners) == null ? void 0 : _a.onChange)) {
            const tmp = [];
            if (import_react.default.isValidElement(icon)) {
              tmp.push(icon);
            } else if (import_icon.IUPIcon[icon]) {
              tmp.push(
                import_react.default.createElement(import_icon.IUPIcon[icon], {
                  key: tmp.length + 1,
                  style: {
                    color: "var(--primary-color)",
                    marginRight: 4,
                    ...style
                  }
                })
              );
            }
            showTip && tmp.push(showTip);
            tmp.push(showTip ? `(${value})` : value);
            return import_react.default.createElement("a", {
              children: tmp,
              async onClick() {
                var _a2;
                const { listeners, antProps, xtype, disabled, hidden, ...athProps } = editor || {};
                const tf = await ((_a2 = listeners == null ? void 0 : listeners.onBeforeInit) == null ? void 0 : _a2.call(listeners, { ...params }));
                const reslut = await import_util.iup.external.openAttachment({
                  asrSessionGuid: v,
                  asrCode: v,
                  asrTable: v,
                  colAttach: 1,
                  disabled: tf !== false && (listeners == null ? void 0 : listeners.onChange) ? false : true,
                  fileTypeCode,
                  ...athProps,
                  ...antProps
                });
                if (tf !== false) {
                  if (listeners == null ? void 0 : listeners.onChange) {
                    const lastRow = { ...row };
                    row[import_common.AttachmentTemp] = { ...row[import_common.AttachmentTemp], [dataIndex]: 1 };
                    listeners.onChange(reslut, { ...params, displayField, rowIndex, lastRow });
                  }
                }
              }
            });
          }
          return nullValue;
        };
      case "number":
        return ({ value }) => {
          let tmpStr = "";
          if (import_util.iup.isNullOrEmpty(value)) {
            return nullValue;
          }
          if (import_util.iup.isFunction(formatter)) {
            tmpStr = formatter({ value, precision }) + "";
          } else {
            tmpStr = import_util.iup.isNullOrEmpty(precision) ? import_util.iup.numberPrecision(value) + "" : import_util.iup.numberPrecision(value).toFixed(precision);
            if (formatter !== false) {
              tmpStr = import_util.iup.thousandNumber(tmpStr, { separator: formatter ?? "," });
            }
          }
          return wrapHtml(import_util.iup.isNullOrEmpty(tmpStr) ? nullValue : `${prefix || ""}${tmpStr}${suffix || ""}`);
        };
      case "expr":
        return ({ table, row, value, dataIndex }) => {
          try {
            const fn = new Function("$D", "$R", "$V", "$DI", `return ${formatter}`);
            return wrapHtml(fn(table.getRows(), row, value, dataIndex) ?? nullValue);
          } catch (e) {
            console.warn(`expression error:${formatter}`, e);
          }
          return wrapHtml(value);
        };
      default:
        break;
    }
  } else if (render) {
    const renderFn = import_util.iup.parseJson(render);
    return import_util.iup.isFunction(renderFn) ? renderFn : void 0;
  }
  return void 0;
}
function clientColumnFilter(columnFilters, dataIndexMap, allMatch = true) {
  const filterKeys = Object.keys(columnFilters);
  const matchFn = allMatch ? "every" : "some";
  return (row) => filterKeys[matchFn]((k) => {
    const { dataIndexField, filter } = dataIndexMap[k];
    const value = import_util.iup.getObjValue(row, dataIndexField || k);
    if (import_util.iup.isFunction(filter == null ? void 0 : filter.clientFilter)) {
      return filter.clientFilter({ dataIndex: k, row, value, filterValue: columnFilters[k] });
    }
    if (import_util.iup.isArray(columnFilters[k])) {
      return columnFilters[k].includes(value);
    }
    const str = !import_util.iup.isNullOrEmpty(value) ? (value + "").toLowerCase() : "";
    const kw = (columnFilters[k] + "").toLowerCase();
    return str.includes(kw);
  });
}
function syncDefaultProps(props) {
  const tableConfig = (0, import_util.getGlobalConfig)().default.tableConfig;
  Object.keys(tableConfig).forEach((key) => {
    if (!props.hasOwnProperty(key)) {
      props[key] = tableConfig[key];
    }
  });
  let { type } = props.rowSelection || {};
  type = import_util.iup.isArray(type) ? type : [type];
  if (!props.checkbox && !type.includes(import_common.TableSelectionModel.CHECKBOX)) {
    props.rowChecked = false;
    delete props.tableAlertRender;
  }
  return props;
}
function getCellInfoByEvent() {
  var _a;
  const targetEl = (_a = window.event) == null ? void 0 : _a.target;
  const obj = { rowIndex: -1 };
  if (targetEl) {
    const rowEl = import_util.iup.closest(targetEl, (el) => {
      if (el.dataset.hasOwnProperty("key")) {
        obj.dataIndex = el.dataset.key;
      }
      return el.classList.contains("table-row");
    });
    if (rowEl) {
      for (let cls of rowEl.classList) {
        if (cls.indexOf("index-") === 0) {
          obj.rowIndex = parseInt(cls.replace("index-", ""));
          break;
        }
      }
    }
  }
  return obj;
}
function getDisplayField({ editor, dataIndex, row }) {
  if (editor) {
    let df = (editor == null ? void 0 : editor.nameField) || (editor == null ? void 0 : editor.displayField);
    const xType = import_util.iup.isString(editor == null ? void 0 : editor.xtype) ? editor.xtype.toLowerCase() : "";
    if (editor && !df && (xType.endsWith("select") || xType.endsWith("help"))) {
      df = `${dataIndex}EXName`;
    }
    if (!df && import_util.iup.isFunction(editor == null ? void 0 : editor.xtype) && row.hasOwnProperty(`${dataIndex}EXName`)) {
      df = `${dataIndex}EXName`;
    }
    return df || dataIndex;
  }
  return dataIndex;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  IUPComponent,
  Layout,
  Observer,
  ValueTypeEnum,
  clientColumnFilter,
  compHoc,
  convertStyle,
  cssVar,
  dataSortOrder,
  displayText,
  domContains,
  emptyFn,
  filterSymbol,
  getCellInfoByEvent,
  getColumnHeader,
  getDisplayField,
  getGlobalConfig,
  getRegisterComponentWithProps,
  getRender,
  getScrollBarInfo,
  stopPropagation,
  syncDefaultProps,
  useApi,
  useAsyncEffect,
  useDebounce,
  useDevState,
  useDraggableSort,
  useIUPEffect,
  useRefCallback,
  useRefState,
  useResize,
  useReturnCallback,
  useThrottle,
  useUpdateEffect,
  util
});
